import express from 'express'
import SysUser from '../../modules/sysuser'

const router = express.Router()

/* GET user home page. */
router.get('/', function (req, res) {
  res.redirect('/user/index')
})

router.get('/index', function (req, res, next) {
  res.render('user/index', { name: 'User Index' })
})

// 测试json数据
router.get('/json', function (_req, res, _next) {
  res.json({
    code: 200,
    message: 'success',
    data: [{
      1: 'one'
    }]
  })
})

// 查询所有用户数据
// router.get('/users', function (_req, res, _next) {
//   Users.fetch(function (err, users) {
//     if (err) {
//       console.log(err + 'cauole')
//     }
//     // res.render('users',{title: '用户列表', users: users})
//     // 这里也可以json的格式直接返回数据res.json({data: users});

//     res.json({
//       status: 200,
//       data: users
//     })
//   })
// })

// 查询
router.get('/query', function (_req, res, _next) {
  SysUser.find({}, (err, result) => {
    if (err) return console.log(err)
    res.json({
      code: 200,
      data: result
    })
    console.log(result)
  })
})

router.get('/create', (_req, res, _next) => {
  const newUser = [{
    userAccount: 'ac001',
    userPassword: '123456',
    userName: '爱德华'
  }]
  SysUser.create(newUser, err => {
    if (err) return console.log(err)
    res.json({
      code: 200,
      message: '创建成功'
    })
  })
})

// 登录
router.post('/login', (req, res) => {
  // console.log(req.body)
  const account = req.body.userAccount
  const password = req.body.userPassword
  SysUser.find({
    userAccount: account,
    userPassword: password,
    deleteFlag: 0
  }).countDocuments((_err, count) => {
    if (count > 0) {
      SysUser.find({
        userAccount: account,
        userPassword: password,
        deleteFlag: 0
      }, (_err, docs) => {
        console.log('Login User: ' + docs)
        if (docs[0].activeFlag === 0) {
          res.send({
            code: 100,
            message: '登录成功',
            data: docs[0]
          })
        } else {
          res.send({
            code: 102,
            message: '该用户被禁用'
          })
        }
      })
    } else {
      res.send({
        code: 101,
        message: '账号或密码错误'
      })
    }
  })
})

// 用户查询
router.get('/list', (req, res) => {
  const searchjson = req.query
  const obj = {
    deleteFlag: 0
  }
  let num = 0
  let numSkip = 0
  let total = 0
  if (searchjson.pageSize >= 1) {
    num = parseInt(searchjson.pageSize)
    numSkip = parseInt(searchjson.pageSize) * parseInt(searchjson.pageNum - 1)
  }
  if (searchjson.userAccount !== '') {
    obj.userAccount = {
      $regex: searchjson.userAccount,
      $options: 'i'
    }
  }
  if (searchjson.userName !== '') {
    obj.userName = {
      $regex: searchjson.userName,
      $options: 'i'
    }
  }
  SysUser.find(obj).countDocuments((_err, count) => {
    total = count
    if (total > 0) {
      SysUser.find(obj, (err, docs) => {
        if (err) {
          res.send({
            code: 101,
            message: '用户查询失败'
          })
        } else {
          res.send({
            code: 100,
            message: '用户查询成功',
            data: {
              list: docs,
              total
            }
          })
        }
      }).skip(numSkip).limit(num).sort({
        updateTime: -1
      })
    }
  })
})

// 用户添加
router.post('/add', (req, res) => {
  const userjson = req.body
  delete userjson._id
  const user = new SysUser(userjson)
  user.save((err, _docs) => {
    if (err) {
      res.send({
        code: 101,
        message: '用户添加失败'
      })
    } else {
      res.send({
        code: 100,
        message: '用户添加成功'
      })
    }
  })
})

// 用户角色修改
router.put('/role/update', (req, res) => {
  const id = req.body._id
  const userRoles = req.body.userRoles
  SysUser.findByIdAndUpdate({
    _id: id
  }, {
    userRoles
  }, (err, _docs) => {
    if (err) {
      res.send({
        code: 101,
        message: '用户角色修改失败'
      })
    } else {
      res.send({
        code: 100,
        message: '用户角色修改成功'
      })
    }
  })
})

// 用户删除
router.delete('/del', (req, res) => {
  const id = req.body._id
  SysUser.findByIdAndUpdate({
    _id: id
  }, {
    deleteFlag: 1
  }, (err, _docs) => {
    if (err) {
      res.send({
        code: 101,
        message: '用户删除失败'
      })
    } else {
      res.send({
        code: 100,
        message: '用户删除成功'
      })
    }
  })
})

export default router
